package com.zxp.zxpsmarthomeapk.view.activity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.zxp.zxpsmarthomeapk.R;
import com.zxp.zxpsmarthomeapk.base.CommentActivity;
import com.zxp.zxpsmarthomeapk.communicate.CommunicateServer;
import com.zxp.zxpsmarthomeapk.communicate.SendAsyncTask;
import butterknife.BindView;

/**
 * 测试模块和app是否可以正常通信
 */
public class CommunicationActivity extends CommentActivity implements View.OnClickListener{
    @BindView(R.id.tv_content)
    TextView tv_content;
    @BindView(R.id.btn_send)
    Button btn_send;
    @BindView(R.id.tv_send_text)
    TextView tv_send_text;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_communication;
    }

    @Override
    protected void initData() {
        //开启服务器
        CommunicateServer communicateServer = new CommunicateServer();
        communicateServer.setHandler(handler);
        new Thread(communicateServer).start();
    }

    @Override
    protected void initView() {
        btn_send.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_send:
                String str = "Sent to the ESP8266";
                new SendAsyncTask().execute(str);
                tv_send_text.setText(str);
                break;
                default:break;
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    tv_content.setText("WiFi模块发送的：" + msg.obj);
                    Toast.makeText(CommunicationActivity.this, "接收到信息", Toast.LENGTH_LONG)
                            .show();
            }
        }
    };
}
